package auctionhaus



import grails.test.mixin.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Listing)
@Mock([Customer, Bid])
class ListingTests {
    def newbid
    def seller

    // setup objects for listing unit tests
    public void setUp() {
        mockForConstraintsTests(Listing)
        newbid = new Bid(
                amount: 1,
                timeOfBid: new Date()
        )
        seller = new Customer(
                email: "johndoe@acme.com",
                password: "1234567"
        )
    }

    // test name format
    void testNameConstraints() {
        // test blank name
        def blankListing = new Listing(
                endTime: new Date() + 20,
                seller: seller,
                startBid: 1
        )
        assert !blankListing.validate()

        // test too long of a name
        def longListing = new Listing(
                name: "12345678901234567890123456789012345678901234567890123456789012345",
                endTime: new Date() + 20,
                seller: seller,
                startBid: 1
        )
        assert !longListing.validate()

        // test name at exactly 64
        def exactListing = new Listing(
                name: "1234567890123456789012345678901234567890123456789012345678901234",
                endTime: new Date() + 20,
                seller: seller,
                startBid: 1
        )
        assert exactListing.validate()

        // test normal listing
        def normalListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 20,
                seller: seller,
                startBid: 1
        )
        assert normalListing.validate()
    }

    // test end time format
    void testEndTimeConstraints() {
        //test blank end date
        def blankTimeListing = new Listing(
                name: "My awesome pocket lint!",
                seller: seller,
                startBid: 1
        )
        assert !blankTimeListing.validate()
        
        // test past date
        def pastTimeListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() - 1,
                seller: seller,
                startBid: 1
        )
        assert !pastTimeListing.validate()
        
        // test today's date
        def nowTimeListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date(),
                seller: seller,
                startBid: 1
        )
        assert !nowTimeListing.validate()
        
        // test future date
        def futureTimeListing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 1,
                seller: seller,
                startBid: 1
        )
        assert futureTimeListing.validate()
    }

    // test description format
    void testDescriptionConstraints() {
        // test blank description
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
                startBid: 1
        )
        assert listing.validate()
        
        // test over 256 character limit
        listing.description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ligula dui, vehicula ac vulputate ut, bibendum scelerisque dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris sagittis bibendum orci ut venen"
        assert !listing.validate()

        // test at 256 character limit
        listing.description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ligula dui, vehicula ac vulputate ut, bibendum scelerisque dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris sagittis bibendum orci ut v"
        assert listing.validate()

        // test a normal description
        listing.description = "Etiam fermentum consectetur varius. Aenean et leo nec dolor eleifend bibendum. In dictum neque et erat egestas a fermentum mauris posuere."
        assert listing.validate()
    }

    // test seller creation
    void testSellerConstraints() {
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                startBid: 1
        )
        assert !listing.validate()

        listing.seller = seller
        assert listing.validate()
    }

    // test winner is require
    void testNullableWinner() {
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
                startBid: 1
        )
        assert listing.validate()
    }

    // test start bid format
    void testStartBidConstraints() {
        def listing = new Listing(
                name: "My awesome pocket lint!",
                endTime: new Date() + 5,
                seller: seller,
        )
        assert !listing.validate()

        listing.startBid = 1
        assert listing.validate()
    }

    // clean up after listing unit tests
    public void tearDown() {
        newbid = null
        seller = null
    }
}
